package com.xinzhi.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);

    private static RedisTemplate<String, Object> redisTemplate; // 移除了@Autowired修饰符

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        RedisUtils.redisTemplate = redisTemplate;
    }

    public static boolean expire(String key, long time) {
        try {
            logger.info("RedisCmd==================expire key:" + key);
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================expire error:" + e.getMessage());
            return false;
        }
    }

    public static long getExpire(String key) {
        logger.info("RedisCmd==================getExpire key:" + key);
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    public static boolean hasKey(String key) {
        try {
            logger.info("RedisCmd==================hasKey key:" + key);
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            logger.error("RedisCmd==================hasKey error:" + e.getMessage());
            return false;
        }
    }

    public static Set<String> keys(String key) {
        logger.info("RedisCmd==================keys key:" + key);
        return key == null ? null : redisTemplate.keys(key);
    }

    public static Object get(String key) {
        logger.info("RedisCmd==================get key:" + key);
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    public static boolean set(String key, Object value) {
        try {
            logger.info("RedisCmd==================set1 key:" + key);
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================set1 error:" + e.getMessage());
            return false;
        }
    }

    public static boolean set(String key, Object value, long time) {
        try {
            logger.info("RedisCmd==================set2 key:" + key);
            if (time > 0) {
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================set2 error:" + e.getMessage());
            return false;
        }
    }

    public static long incr(String key, long delta) {
        if (delta < 0) {
            throw new IllegalArgumentException("递增因子必须大于0");
        }
        logger.info("RedisCmd==================incr key:" + key);
        return redisTemplate.opsForValue().increment(key, delta);
    }

    public static long decr(String key, long delta) {
        if (delta < 0) {
            throw new IllegalArgumentException("递减因子必须大于0");
        }
        logger.info("RedisCmd==================decr key:" + key);
        return redisTemplate.opsForValue().increment(key, -delta);
    }

    public static Object hget(String key, String item) {
        logger.info("RedisCmd==================hget key:" + key);
        return redisTemplate.opsForHash().get(key, item);
    }

    public static Map<Object, Object> hmget(String key) {
        logger.info("RedisCmd==================hmget key:" + key);
        return redisTemplate.opsForHash().entries(key);
    }

    public static boolean hmset(String key, Map<String, Object> map) {
        try {
            logger.info("RedisCmd==================hmset1 key:" + key);
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================hmset1 error:" + e.getMessage());
            return false;
        }
    }

    public static boolean hmset(String key, Map<String, Object> map, long time) {
        try {
            logger.info("RedisCmd==================hmset2 key:" + key);
            redisTemplate.opsForHash().putAll(key, map);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================hmset2 error:" + e.getMessage());
            return false;
        }
    }

    public static boolean hset(String key, String item, Object value) {
        try {
            logger.info("RedisCmd==================hset1 key:" + key);
            redisTemplate.opsForHash().put(key, item, value);
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================hset1 error:" + e.getMessage());
            return false;
        }
    }

    public static boolean hset(String key, String item, Object value, long time) {
        try {
            logger.info("RedisCmd==================hset2 key:" + key);
            redisTemplate.opsForHash().put(key, item, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================hset2 error:" + e.getMessage());
            return false;
        }
    }

    public static void hdel(String key, Object... item) {
        logger.info("RedisCmd==================hdel key:" + key);
        redisTemplate.opsForHash().delete(key, item);
    }

    public static boolean hHasKey(String key, String item) {
        logger.info("RedisCmd==================hHasKey key:" + key);
        return redisTemplate.opsForHash().hasKey(key, item);
    }

    public static double hincr(String key, String item, double by) {
        logger.info("RedisCmd==================hincr key:" + key);
        return redisTemplate.opsForHash().increment(key, item, by);
    }

    public static double hdecr(String key, String item, double by) {
        logger.info("RedisCmd==================hdecr key:" + key);
        return redisTemplate.opsForHash().increment(key, item, -by);
    }

    public static Set<Object> sGet(String key) {
        try {
            logger.info("RedisCmd==================sGet key:" + key);
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            logger.error("RedisCmd==================sGet error:" + e.getMessage());
            return null;
        }
    }

    public static boolean sHasKey(String key, Object value) {
        try {
            logger.info("RedisCmd==================sHasKey key:" + key);
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            logger.error("RedisCmd==================sHasKey error:" + e.getMessage());
            return false;
        }
    }

    public static long sSet(String key, Object... values) {
        try {
            logger.info("RedisCmd==================sSet key:" + key);
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            logger.error("RedisCmd==================sSet error:" + e.getMessage());
            return 0;
        }
    }

    public static long sSetAndTime(String key, long time, Object... values) {
        try {
            logger.info("RedisCmd==================sSetAndTime key:" + key);
            Long count = redisTemplate.opsForSet().add(key, values);
            if (time > 0)
                expire(key, time);
            return count;
        } catch (Exception e) {
            logger.error("RedisCmd==================sSetAndTime error:" + e.getMessage());
            return 0;
        }
    }

    public static long sGetSetSize(String key) {
        try {
            logger.info("RedisCmd==================sGetSetSize key:" + key);
            return redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            logger.error("RedisCmd==================sGetSetSize error:" + e.getMessage());
            return 0;
        }
    }

    public static long setRemove(String key, Object... values) {
        try {
            logger.info("RedisCmd==================setRemove key:" + key);
            return redisTemplate.opsForSet().remove(key, values);
        } catch (Exception e) {
            logger.error("RedisCmd==================setRemove error:" + e.getMessage());
            return 0;
        }
    }

    public static List<Object> lGet(String key, long start, long end) {
        try {
            logger.info("RedisCmd==================lGet key:" + key);
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            logger.error("RedisCmd==================lGet error:" + e.getMessage());
            return null;
        }
    }

    public static long lGetListSize(String key) {
        try {
            logger.info("RedisCmd==================lGetListSize key:" + key);
            return redisTemplate.opsForList().size(key);
        }         catch (Exception e) {
            logger.error("RedisCmd==================lGetListSize error:" + e.getMessage());
            return 0;
        }
    }

    public static Object lGetIndex(String key, long index) {
        try {
            logger.info("RedisCmd==================lGetIndex key:" + key);
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            logger.error("RedisCmd==================lGetIndex error:" + e.getMessage());
            return null;
        }
    }

    public static boolean lSet(String key, Object value) {
        try {
            logger.info("RedisCmd==================lSet1 key:" + key);
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================lSet1 error:" + e.getMessage());
            return false;
        }
    }

    public static boolean lSet(String key, Object value, long time) {
        try {
            logger.info("RedisCmd==================lSet2 key:" + key);
            redisTemplate.opsForList().rightPush(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================lSet2 error:" + e.getMessage());
            return false;
        }
    }

    public static boolean lSet(String key, List<Object> value) {
        try {
            logger.info("RedisCmd==================lSet3 key:" + key);
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================lSet3 error:" + e.getMessage());
            return false;
        }
    }

    public static boolean lSet(String key, List<Object> value, long time) {
        try {
            logger.info("RedisCmd==================lSet4 key:" + key);
            redisTemplate.opsForList().rightPushAll(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================lSet4 error:" + e.getMessage());
            return false;
        }
    }

    public static boolean lUpdateIndex(String key, long index, Object value) {
        try {
            logger.info("RedisCmd==================lUpdateIndex key:" + key);
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            logger.error("RedisCmd==================lUpdateIndex error:" + e.getMessage());
            return false;
        }
    }

    public static long lRemove(String key, long count, Object value) {
        try {
            logger.info("RedisCmd==================lRemove key:" + key);
            return redisTemplate.opsForList().remove(key, count, value);
        } catch (Exception e) {
            logger.error("RedisCmd==================lRemove error:" + e.getMessage());
            return 0;
        }
    }
}
